<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of UserAction
 *
 * @author cb
 */
class UserAction extends Action {

    //put your code here
    public function index() {
        $uid = $_GET['uid'];
        $db = D('UserView');
        $arr = $db->where(array('uid' => $uid))->find();
        $this->assign('arr', $arr);
        $this->display();
    }

    public function edit() {
        $uname = $_POST['uname'];
        $db = M('user');
        $arr = $db->where(array('uname' => $uname, 'uid' => array('neq', $_SESSION['uid'])))->find();
        if ($arr) {
            echo json_encode(array('num' => 0));
            die;
        }
        $_POST['uid'] = $_SESSION['uid'];
        if ($db->save($_POST)) {
            $db = M('userinfo');
            if ($db->where(array('pid' => $_SESSION['uid']))->save($_POST)) {
                echo json_encode(array('num' => 1));
            } else {
                echo json_encode(array('num' => 2));
            }
        } else {
            echo json_encode(array('num' => 2));
        }
    }
//上传头像
    public function upload() {
        import('ORG.Net.UploadFile');
        $file = './static/' . date("ymdH", time());
        is_dir($file) || mkdir($file, 0777, true);
        $upload = new UploadFile();
        $upload->saveRule = time();
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');
        $upload->savePath = $file . "/";
        $upload->upload();
        $arr = $upload->getUploadFileInfo();
        $file = "/huaban/index" . substr($arr[0]['savepath'], 1) . $arr[0]['savename'];
        $db = M('user');
        $db->where('uid='.$_GET['uid'])->save(array('thump'=>$file));
        $_SESSION['thump'] = $file;
        echo $file;
    }
  //修改登录邮箱
    public function username(){
        $username = htmlspecialchars($_POST['username']);
        $password = htmlspecialchars($_POST['password']);
        $db = M('user');
        $arr = $db->where(array('username'=>$username,'uid'=>array('neq',$_SESSION['uid'])))->find();
        if($arr){
            echo json_encode(array('num'=>0));die; 
        }
        if($db->where(array('password'=>$password,'uid'=>$_SESSION['uid']))->save(array('username'=>$username))){
            echo json_encode(array('num'=>1)); 
        }else{
            echo json_encode(array('num'=>2)); 
        }
    }
    //修改密码
    public function pass(){
        $old = htmlspecialchars($_POST['password']);
        $new = htmlspecialchars($_POST['new']);
        $db = M('user');
          if($db->where(array('password'=>$old,'uid'=>$_SESSION['uid']))->save(array('password'=>$new))){
            echo json_encode(array('num'=>1)); 
        }else{
            echo json_encode(array('num'=>0)); 
        }
    }

}

?>
